package concurrent;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description: TODO 轻量级锁/可重入锁 ReentrantLock
 * @Author: wzj
 * @Date: 2020/6/15 11:56
 */
public class LockDemo {
    private Lock lock=new ReentrantLock();

    public void lock(String name) {
        System.out.println(name+" 加锁之前");
        lock.lock();//这条语句以下开始加锁
        System.out.println(name+" 加锁");
        try {
            Thread.sleep(2000);
            System.out.println(name+" 还在加锁");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();//这条语句之后解锁，unlock()之后，获取锁的线程就可以立即进入lock()之后的程序，不用等unlock()后面的语句执行完
            System.out.println(name+" 解锁");
            try {
                Thread.sleep(2000);
                System.out.println(name+" 解锁，睡眠后");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    public void tryLock(String name){
        if(lock.tryLock()){//获取锁
            System.out.println(name+" 加锁");
            try {
                System.out.println(name+" 还在加锁");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                lock.unlock();//如果这里不释放锁，别的线程永远无法获取锁
                System.out.println(name+" 解锁");
                try {
                    Thread.sleep(2000);
                    System.out.println(name+" 解锁，睡眠后");
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } else {
            System.out.println(name+" 未获取锁");
        }
    }

    public void tryLockTime(String name){
        try {
            if(lock.tryLock(1L, TimeUnit.SECONDS)){//尝试1s内获取锁
                System.out.println(name+" 加锁");
                try {
                    Thread.sleep(1000);
                    System.out.println(name+" 还在加锁");
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    lock.unlock();//如果这里不释放锁，别的线程永远无法获取锁
                    System.out.println(name+" 解锁");
                    try {
                        Thread.sleep(2000);
                        System.out.println(name+" 解锁，睡眠后");
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            } else {
                System.out.println(name+" 未获取锁");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }



    public static void main(String[] args) {
        LockDemo lockDemo=new LockDemo();
        for(int i=0;i<5;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    lockDemo.lock(Thread.currentThread().getName());
//                    lockDemo.tryLock(Thread.currentThread().getName());
//                    lockDemo.tryLockTime(Thread.currentThread().getName());
                }
            }).start();
        }
    }
}
